<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app'
import type { IBannerItem, ICategoryMutliItem, HotItem } from '@/types/home'
import { getHomeBannerAPI, getHomeCategoryMutliAPI, getHomeHotAPI } from '@/services/home'
import CustomNavbar from './componets/CustomNavbar.vue'
import CategoryPanel from './componets/CategoryPanel.vue'
import HotPanel from './componets/HotPanel.vue'
import PageSkeleton from './componets/PageSkeleton.vue'

import type { DiyGuessInstance } from '@/types/component'

import { ref } from 'vue'
import { useGuessList } from '@/composables'

const bannerList = ref<IBannerItem[]>([])
const cateList = ref<ICategoryMutliItem[]>([])
const hotlist = ref<HotItem[]>([])
const isTriggered = ref(false)

const isLoading = ref(false)

const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI(1)
  console.log(res, '####')
  bannerList.value = res.result
}

const getHomeCategoryMutliData = async () => {
  const res = await getHomeCategoryMutliAPI()
  console.log(res, '####')
  cateList.value = res.result
}

const getHomeHotData = async () => {
  const res = await getHomeHotAPI()
  console.log(res, '####')
  hotlist.value = res.result
}

// 获取猜你喜欢示例
// const guessRef = ref<DiyGuessInstance>()
// const onScrolltolower = () => {
//   console.log('滚动触底了')
//   guessRef.value?.getMore()
// }
// 猜你喜欢组合式函数
const { guessRef, onScrolltolower } = useGuessList()

// 自定义上拉刷新
const onRefresherrefresh = async () => {
  isTriggered.value = true

  guessRef.value?.resetData()
  await Promise.all([
    getHomeBannerData(),
    getHomeCategoryMutliData(),
    getHomeHotData(),
    guessRef.value?.getMore(),
  ])
  // await getHomeBannerData()
  // await getHomeCategoryMutliData()
  // await getHomeHotData()
  isTriggered.value = false
}

onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryMutliData(), getHomeHotData()])
  isLoading.value = false
})
// onLoad(options) {}
</script>

<template>
  <!-- 自定义导航 -->
  <CustomNavbar />

  <scroll-view
    class="scroll-view10"
    :refresher-enabled="true"
    :refresher-triggered="isTriggered"
    @refresherrefresh="onRefresherrefresh"
    @scrolltolower="onScrolltolower"
    scroll-y
  >
    <!-- 骨架 -->
    <PageSkeleton v-if="isLoading"></PageSkeleton>

    <template v-else>
      <!-- 轮播图 -->
      <DiySwiper :list="bannerList" />

      <!-- 分类 -->
      <CategoryPanel :list="cateList" />

      <!-- 热门推荐 -->
      <HotPanel :list="hotlist" />

      <!-- 猜你喜欢 -->
      <DiyGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
//
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view10 {
  flex: 1;
}
</style>
